package cn.cdeden.aiqa.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.cdeden.common.idempotent.annotation.RepeatSubmit;
import cn.cdeden.common.log.annotation.Log;
import cn.cdeden.common.web.core.BaseController;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.validate.AddGroup;
import cn.cdeden.common.core.validate.EditGroup;
import cn.cdeden.common.log.enums.BusinessType;
import cn.cdeden.common.excel.utils.ExcelUtil;
import cn.cdeden.aiqa.domain.vo.WikiPromptVo;
import cn.cdeden.aiqa.domain.bo.WikiPromptBo;
import cn.cdeden.aiqa.service.IWikiPromptService;
import cn.cdeden.common.core.utils.page.TableDataInfo;

/**
 * 提示词管理
 * 前端访问路由地址为:/aiqa/prompt
 *
 * @author Nick
 * @date 2025-01-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/prompt")
public class WikiPromptController extends BaseController {

    private final IWikiPromptService wikiPromptService;

    /**
     * 查询提示词管理列表
     */
    @SaCheckPermission("aiqa:prompt:list")
    @GetMapping("/list")
    public TableDataInfo<WikiPromptVo> list(WikiPromptBo bo, PageQuery pageQuery) {
        return wikiPromptService.queryPageList(bo, pageQuery);
    }

    /**
     * 客户端 查询提示词列表
     */
    @GetMapping("/get")
    public TableDataInfo<WikiPromptVo> get(WikiPromptBo bo, PageQuery pageQuery) {
        return wikiPromptService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出提示词管理列表
     */
    @SaCheckPermission("aiqa:prompt:export")
    @Log(title = "提示词管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WikiPromptBo bo, HttpServletResponse response) {
        List<WikiPromptVo> list = wikiPromptService.queryList(bo);
        ExcelUtil.exportExcel(list, "提示词管理", WikiPromptVo.class, response);
    }

    /**
     * 获取提示词管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("aiqa:prompt:query")
    @GetMapping("/{id}")
    public R<WikiPromptVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wikiPromptService.queryById(id));
    }

    /**
     * 新增提示词管理
     */
    @SaCheckPermission("aiqa:prompt:add")
    @Log(title = "提示词管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WikiPromptBo bo) {
        return toAjax(wikiPromptService.insertByBo(bo));
    }

    /**
     * 修改提示词管理
     */
    @SaCheckPermission("aiqa:prompt:edit")
    @Log(title = "提示词管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WikiPromptBo bo) {
        return toAjax(wikiPromptService.updateByBo(bo));
    }

    /**
     * 删除提示词管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("aiqa:prompt:remove")
    @Log(title = "提示词管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wikiPromptService.deleteWithValidByIds(List.of(ids), true));
    }
}
